<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Config;
use app\common\model\PartnerLog;
use app\common\model\Task as TaskModel;

class Task extends Api{

    //protected $noNeedLogin = ['getList'];
    protected $noNeedRight = '*';

    public function getList(){
        $type = request()->get('type',1);
        $list = TaskModel::where('status',1)->where('types',$type)->field('id,name,money,types,url,created_at')->order('weigh','desc')->select();
        foreach($list as $v){
            if($this->auth->id){
                $v['status'] = TaskModel::checkCom($v,$this->auth->id);
            }else{
                $v['status'] = 0;
            }
        }
        $this->success('请求成功',$list);
    }

    /**
     * 领取
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receive(){
        $taskId = request()->post('task_id');
        $task = TaskModel::where('id',$taskId)->where('status',1)->find();
        if(TaskModel::checkCom($task,$this->auth->id) != 1){
            $this->error('任务未完成');
        }
        $result = TaskModel::receive($task,$this->auth->id);
        if(!$result){
            $this->error('领取失败');
        }
        $this->success('领取成功');
    }
}